//
//  WelcomeViewController.swift
//  微博项目
//
//  Created by zhaoyinpeng on 16/3/12.
//  Copyright © 2016年 zhaoyinpeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class WelcomeViewController: UIViewController {
    
    
    override func loadView() {
        //.大小会自动设置
        view = backImageView
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    
    //.设置UI
    private func setupUI() {
        
        //.添加子视图
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            
            make.height.equalTo(90)
            make.width.equalTo(90)
            
            
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            make.centerX.equalTo(iconImageView.snp_centerX)
        }
        
        //.设置图片 
        
        iconImageView.sd_setImageWithURL(UserAccount.shareAccout.headImageURL)
        
        //.设置layer相关的属性
        iconImageView.layer.cornerRadius = 45
        iconImageView.layer.masksToBounds = true
        iconImageView.layer.borderWidth = 3
        iconImageView.layer.borderColor = UIColor.purpleColor().CGColor
        
        welcomeLabel.alpha = 0
        
    }
    
    //.界面已经出现的时候会自动调用 这个方法是页面显示之后的生命周期方法 在这个方法中所有的子视图的位置已经设置完毕
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    
    private func startAnimation() {
        
        let offsetY = -UIScreen.mainScreen().bounds.height + 180
        
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offsetY)
        }
        
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //.强制刷新页面
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("ok")
                   //.切换控制器
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
                        
                        
                })
        }
        
    }
    
    
    //.懒加载子视图
    private lazy var backImageView = UIImageView(image:UIImage(named: "ad_background"))
    private lazy var iconImageView = UIImageView(image:UIImage(named: "avatar_default_big"))

    private lazy var welcomeLabel: UILabel = UILabel.labelWith(UIColor.darkGrayColor(), fontSize: 16, text: (UserAccount.shareAccout.name ?? "") + " 欢迎回来")
    
    
}
